package org.bbelovic.devel.pexeso.gui;

import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class FlagLabelsManager {

    private static final String IMAGES_DIRECTORY = "/images/";
    private static final String BLACK_ICON = IMAGES_DIRECTORY + "black.GIF";
    private static final String GREY_ICON = IMAGES_DIRECTORY + "grey.GIF";
    private final Map<String, JLabel> labelMap;
    private MouseListener ml;

    public FlagLabelsManager() {
        this (new HashMap<String, JLabel>());
    }

    FlagLabelsManager(final Map<String, JLabel> labelMap) {
        this.labelMap = labelMap;
    }

    Collection<JLabel> prepareFlagLabels() {
        final List<String> imageNames = readImageNames();
        for (final String eachName: imageNames) {
            int id = 0;
            String labelId = eachName + "-" + (++id);
            JLabel label = prepareFlagLabel(ml, labelId);
            labelMap.put(labelId, label);
            labelId = eachName + "-" + (++id);
            label = prepareFlagLabel(ml, labelId);
            labelMap.put(labelId, label);
        }
        List<JLabel> labels = new ArrayList<JLabel>(labelMap.values());
        Collections.shuffle(labels);
        return labels;
    }

    void turnBlackIconOn() {
        final Icon black = new ImageIcon(getClass().getResource(BLACK_ICON));
        for (Map.Entry<String, JLabel> eachEntry: labelMap.entrySet()) {
            JLabel label = eachEntry.getValue();
            label.setIcon(black);
        }
    }

    void turnFlagIconOn(final String labelId) {
        final String imageName = labelId.substring(0, labelId.indexOf("-"));
        final Icon flagIcon = new ImageIcon(getClass().getResource(imageName));
        final JLabel label = labelMap.get(labelId);
        label.setIcon(flagIcon);
    }

    void removeFlagLabelWithId(final Collection<String> labelIds) {
        for (final String eachLabelId: labelIds) {
            if (labelMap.containsKey(eachLabelId)) {
                labelMap.remove(eachLabelId);
            }
        }
    }

    boolean isFlagIconAvailable(final String flagIconName) {
        return labelMap.containsKey(flagIconName);
    }

    void setMouseListener(final MouseListener ml) {
        this.ml = ml;
    }

    Map<String, JLabel> getLabelMap() {
        return labelMap;
    }

    private JLabel prepareFlagLabel(final MouseListener ml, final String eachName) {
        JLabel label = new JLabel();
        label.setName(eachName);
        label.setIcon(new ImageIcon(getClass().getResource(BLACK_ICON)));
        label.addMouseListener(ml);
        return label;
    }

    private List<String> readImageNames() {
        final List<String> fileNames = Utilities.getImagePaths();
        final List<String> names = new ArrayList<String>();
        for (String eachName: fileNames) {
            if (isValidImageName(eachName)) {
                names.add(eachName);
            }
        }
        return names;
    }
    
    private boolean isValidImageName(final String imageName) {
        return imageName.startsWith("/images/") && !imageName.equals(BLACK_ICON) && !imageName.equals(GREY_ICON);
    }
}
